<?php

header('Content-type: text/json');

$q = ( $_GET['q'] ? strtolower( $_GET['q'] ) : null );

if($q):
	$list = json_decode( file_get_contents('search.json') );

	$filtered = array_filter($list->data, function($item){
		//print_r( strtolower( $_GET['q'] ) );
		//print_r( strpos( strtolower( $item->desc ), strtolower( $_GET['q'] ) ) );

		if ( strpos( strtolower($item->url), strtolower( $_GET['q'] ) ) !== false ):
	        return true;
		endif;

		if ( strpos( strtolower($item->description), strtolower( $_GET['q'] ) ) !== false ):
	        return true;
		endif;

		foreach( $item->tags as $tag ):
			if ( strpos( strtolower($tag), strtolower( $_GET['q'] ) ) !== false ):
		        return true;
			endif;
		endforeach;

		foreach( $item->urls as $url ):
			if ( strpos( strtolower($url->title), strtolower( $_GET['q'] ) ) !== false ):
		        return true;
			endif;
		endforeach;

	    return false;
	});

	print_r( json_encode( $filtered ) );
endif;


?>